home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / languages / fpl-v115.lha / FPL / funclib / func.c next >
Encoding:
C/C++ Source or Header  |  1994-05-10  |  3.5 KB  |  146 lines

  1. /********************************************************************
  2.  *
  3.  * This is a small program that starts and stops another program.
  4.  * The 'other program' is supposed to add functions to FPL at startup
  5.  * with the given anchor, and is also supposed to remove them when
  6.  * told to close down.
  7.  *
  8.  * This program can be made pure/re-entrant.
  9.  *
  10.  */
  11. #include <proto/exec.h>
  12. #include <proto/dos.h>
  13. #include <exec/execbase.h>
  14. #include <exec/ports.h>
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #include "funclib.h"
  21.  
  22. #define OURPORTNAME "FPLfunc"
  23. #define OTHERPORTNAME "FPLlib"
  24.  
  25. #define VERSION 2 /* we say that this is the version 2 of the funclib */
  26.  
  27. struct ReportMsg {
  28.   struct Message msg;
  29.   int ErrorNumber;
  30. };
  31.  
  32. extern struct ExecBase *SysBase;
  33.  
  34. void CloseOurPort(struct MsgPort *port)
  35. {
  36.   if (port){
  37.     RemPort(port);
  38.     DeleteMsgPort(port);
  39.   }
  40. }
  41.  
  42. FuncRet main(int argc, char *argv[])
  43. {
  44.   long open=FALSE;
  45.   long close=FALSE;
  46.   long version;
  47.   long anchor;
  48.   char ourportname[40];
  49.   char otherportname[40];
  50.   char systemline[80];
  51.   struct MsgPort *port;
  52.   struct MsgPort *otherport;
  53.   if(argc<3)
  54.     return(FUNCLIB_PARAMETER);
  55.  
  56.   if(!strcmp("open", argv[1])) {
  57.     open = TRUE;
  58.     version = atoi(argv[3]);
  59.     if (version > VERSION)
  60.       /* the requested version is larger than this is, fail! */
  61.       return FUNCLIB_VERSION;
  62.   } else {
  63.     close = TRUE;
  64.   }
  65.   anchor = atoi(argv[2]);
  66.  
  67.   sprintf(ourportname, "%s.%d", OURPORTNAME, anchor);
  68.   sprintf(otherportname, "%s.%d", OTHERPORTNAME, anchor);
  69.  
  70.   /* Make our port, so that the funclib can find us */
  71.   if (port=CreateMsgPort()){
  72.     port->mp_Node.ln_Name=ourportname;
  73.     port->mp_Node.ln_Pri=0;
  74.     AddPort(port);
  75.   } else
  76.     return FUNCLIB_INTERNAL; /* We failed... */
  77.  
  78.   if(open) {
  79.     /*
  80.      * Search for an already running funclib.
  81.      */
  82.     Forbid();
  83.     otherport = FindPort(otherportname);
  84.     Permit();
  85.     if (!otherport) {
  86.       /*
  87.        * Start the funclib program.
  88.        * We send it the 'version' and 'anchor' parameters as we know
  89.        * about.
  90.        */
  91.       sprintf(systemline,
  92.               "run >NIL: FPLLIBS:lib version %d anchor %d",
  93.               version,
  94.               anchor);
  95.       if(-1 != System(systemline, NULL) ) {
  96.         struct ReportMsg *Mymsg;
  97.         WaitPort(port);
  98.         Mymsg = (struct ReportMsg *) GetMsg(port);
  99.         if (!Mymsg->ErrorNumber) {
  100.           ReplyMsg((struct Message *) Mymsg);
  101.         } else {
  102.           /* We failed opening the funclib! */
  103.           CloseOurPort(port);
  104.           return Mymsg->ErrorNumber;
  105.         }
  106.       } else {
  107.         /* We failed opening the funclib! */
  108.         CloseOurPort(port);
  109.         return FUNCLIB_INTERNAL;
  110.       }
  111.     } else {
  112.       /* the funclib is already running! */
  113.       return FUNCLIB_OK;
  114.     }
  115.   } else {
  116.     /* We tell the funclib to close down! */
  117.     struct ReportMsg Closedown;
  118.     Closedown.msg.mn_ReplyPort=port;
  119.     Closedown.ErrorNumber=0;
  120.     Forbid(); /* Search for an already running funclib. */
  121.     if (otherport = FindPort(otherportname)) {
  122.       PutMsg(otherport,(struct Message *) &Closedown);
  123.       Permit();
  124.       /* Now let's wait for a reply */
  125.       WaitPort(port);
  126.       GetMsg(port) ;
  127.       if (Closedown.ErrorNumber) {
  128.         /* Somehow we failed to close */
  129.         CloseOurPort(port);
  130.         return Closedown.ErrorNumber;
  131.       }
  132.     } else {
  133.       /* there is no funclib to close! */
  134.       Permit();
  135.       return FUNCLIB_OK;
  136.     }
  137.   }
  138.  
  139.   /*
  140.    * We close our port.
  141.    */
  142.   CloseOurPort(port);
  143.  
  144.   return FUNCLIB_OK;
  145. }
  146.